app.controller("z-attendanceImport-c", [ '$scope','$rootScope','$z-http','$sce','z-attendanceImport-s','z-edit-menu-s','$filter','$location','downloadUrl',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter,$location,$downloadUrl) {
		$scope.baseUrl = $location.protocol()+"://"+$location.host()+":"+$location.port()+$downloadUrl;
		$scope.importModel = {};
        $scope.viewModel   = {};

        $scope.importModel.attachmentId = null;
        $scope.importModel.uploadStream = {};
        $scope.importModel.messageHideType=false;
        $scope.importModel.successHideType=false;
        $scope.importModel.message=null;
        $scope.importModel.isArray=false;
        $scope.importModel.radioValue="2";

		//查询页面名称
		MenuService.getMenuByUrl({url:'attendanceImport'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});

		
        $scope.importModel.getTemplate = function () {
            $rootScope.isLoading = true;
            Service.getTemplateUrl(function (data) {
                $rootScope.isLoading = false;
                window.location.href       = data;
            }, function () {
                $rootScope.isLoading = false;
                //$rootScope.alertError("系统错误!");
                $scope.importModel.messageHideType=true;
                $scope.importModel.message="系统错误";
                $scope.importModel.isArray=false;
            });
        };

        $scope.importModel.unload = function () {
            $scope.importModel.attachmentId = null;
            $scope.importModel.uploadStream.cancel();
        };

        $scope.importModel.fileUploadComplete    = function (data) {
            $scope.importModel.messageHideType=false;
            $scope.importModel.successHideType=false;
            var dataList=data;
            if (dataList[0]['attachment'] != null) {
                $scope.$apply(function () {
                    $scope.importModel.attachmentId = dataList[0]['attachment']['attachmentId'];
                    $scope.importModel.save();
                });
            }
        };

        $scope.importModel.save = function () {
        	/*$scope.importModel.attachmentId="5";*/
            $rootScope.alertConfirm("确定考勤信息是否导入！",
                function() {
                    if ($scope.importModel.attachmentId != null) {
                        $rootScope.isLoading = true;
                        Service.importAttendance({
                            attachmentId: $scope.importModel.attachmentId
                        }, function (data) {
                            $rootScope.isLoading = false;
                            if (data['result'] == 1) {
                                $scope.importModel.successHideType=true;
                                if(data['success'].length<=0){
                                    $scope.importModel.isArray = false;
                                    $scope.importModel.message = "考勤信息导入完成。";
                                }else{
                                    $scope.importModel.isArray = true;
                                    $scope.importModel.message = data['success'];
                                }
                                $scope.importModel.unload();
                            } else {
                                $scope.importModel.messageHideType=true;
                                $scope.importModel.message="系统错误";
                                $scope.importModel.isArray=false;
                            }
                        }, function (data) {
                            $rootScope.isLoading      = false;
                            $scope.importModel.attachmentId = null;
                            $scope.importModel.uploadStream.cancel();
                            $scope.importModel.messageHideType=true;
                            if (angular.isArray(data['fault'])) {
                                var faultMessage = [data['fault'][0]['message']].concat(data['fault'][0]['errorData']);
                                $scope.importModel.message=faultMessage;
                                $scope.importModel.isArray=true;
                                //$rootScope.alertError(faultMessage);
                            } else {
                                $scope.importModel.message=data['fault']['message'];
                                $scope.importModel.isArray=false;
                                //$rootScope.alertError(data['fault']['message']);
                            }
                        });
                    } else {
                        $scope.importModel.message="请先上传文件!";
                        $scope.importModel.isArray=false;
                    }
                });
        };
} ]);